package com.person.jc.service.base.customer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdCustomerService extends Service {
	
	public final static BdCustomerService me = new BdCustomerService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select *, DATE_FORMAT(create_date, \'%Y-%m-%d\') create_date_text from bd_customer where if_valid = 1");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("custName")))){
			sql.append(" and cust_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("custCode")))){
			sql.append(" and cust_code like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("startDate")))){
			sql.append(" and DATE_FORMAT(create_date,'%Y-%m-%d') >= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("endDate")))){
			sql.append(" and DATE_FORMAT(create_date,'%Y-%m-%d') <= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("custId")))){
			sql.append(" and cust_id = '"+ temp +"'");
		}
		sql.append(" order by cust_code ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		List<Object> params = new ArrayList<Object>();
		String sql = "insert into bd_customer (cust_name, cust_code,link_man,link_phone,address,remark,link_mobile,link_fax, create_date) values (?, ?, ?, ?, ?, ?, ?, ?, now())";
		params.add(map.get("custName"));
		params.add(map.get("custCode"));
		params.add(map.get("linkMan"));
		params.add(map.get("linkPhone"));
		params.add(map.get("address"));
		params.add(map.get("remark"));
		params.add(map.get("linkMobile"));
		params.add(map.get("linkFax"));
		Db.update(sql, Utils.getObjParam(params));
		super.setCRUD("客户单位管理", "C", "新增客户信息："+map.get("custName"));
	}
	
	public void update(Map<String,Object> map){
		List<Object> params = new ArrayList<Object>();
		String sql = " update bd_customer set cust_name = ? , cust_code = ?, link_man = ? ," +
				" link_phone = ? ,address = ?,remark = ?, link_mobile = ? , link_fax = ?" +
				" where cust_id = ?";
		params.add(map.get("custName"));
		params.add(map.get("custCode"));
		params.add(map.get("linkMan"));
		params.add(map.get("linkPhone"));
		params.add(map.get("address"));
		params.add(map.get("remark"));
		params.add(map.get("linkMobile"));
		params.add(map.get("linkFax"));
		params.add(map.get("custId"));
		Db.update(sql, Utils.getObjParam(params));
		super.setCRUD("客户单位管理", "U", "修改客户信息，编号："+map.get("custId"));
	}
	
	public void delete(String custId){
		String sql = " update bd_customer set if_valid = 0 where cust_id = ? ";
		Db.update(sql, custId);
		super.setCRUD("客户单位管理", "D", "删除客户信息，编号："+custId);
	}
	
	public long checkName(String custCode, String custId, String custName){
		String sql = "select count(1) from bd_customer where if_valid <> 0 ";
		if(Utils.isNotEmpty(custId)){
			sql += " and cust_id <> " + custId;
		}
		if(Utils.isNotEmpty(custCode)){
			sql += " and cust_code = '" + custCode + "'";
		}
		if(Utils.isNotEmpty(custName)){
			sql += " and cust_name = '" + custName + "'";
		}
		return Db.queryLong(sql);
	}
}
